package net.bbtang.website.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.bbtang.website.pojo.UserPublishInfoDTO;
import net.bbtang.website.service.UserPublishInfoService;
import net.bbtang.website.utils.ConstantsStatus;
import net.bbtang.website.utils.StatusCode;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/userPublishInfo")
public class UserPublishInfoController {
	
	@Autowired
	private UserPublishInfoService userPublishInfoServiceImpl;
	

	
	/**发布发布接口
	 @api {post} /userPublishInfo/publish 发布发布接口
	 
	 @apiParam {int} userId 用户编号
	 @apiParam {int} publishType  发布类型 ，悬赏：0，分享：1
	 @apiParam {int} publishPayType  支付类型，现金：0，实物：1
	 @apiParam {String} publishPrice 支付物品（现金是数字，实物是物品描述）
	 @apiParam {String} info 发布内容
	 @apiParam {String} address 发布地址
	 @apiParam {String} contactInformation 联系人电话
	 @apiParam {String} contactName 联系人姓名
	 @apiParam {String} buyerAddress 买家地址
	 @apiParam {int} userGroupId  小组id,(不是小组传入-1)
	

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"userId":10,
		"publishType":5,
		"publishPayType":1
		"publishPrice":"两个事物",
		"info":"好吃的",
		"address":"12楼",
		"contactInformation":"22232321321",
		"contactName":"赵.xn"
		"userGroupId":-1
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 200 OK
	{
	    "flag":false,
	    "number":"详细错误信息",
	    "detailMsg":"错误错误".
	}

 	@apiErrorExample  请求参数异常
		HTTP/1.1 400
 	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author zx
	 */
	@RequestMapping(value = "/publish", method = RequestMethod.POST)
	@ResponseBody
	public Object publish(@RequestBody Object body){
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			Integer userId = null;
			if(!jb.containsKey("userId")){
				throw new Exception("userId is null");
			}else{
				userId = jb.getInt("userId");
			}
			Integer publishType = null;		
			if(!jb.containsKey("publishType")){
				throw new Exception("publishType is null");
			}else{
				publishType = jb.getInt("publishType");
			}
			
			String publishPrice = null;
			if (jb.containsKey("publishPrice")){
				publishPrice = jb.getString("publishPrice");
			}
			
			String info = null;
			if (jb.containsKey("info")){
				info = jb.getString("info");
			}
			
			String address = null;
			if (jb.containsKey("address")){
				address = jb.getString("address");
			}
			
			String contactInformation = null;
			if (jb.containsKey("contactInformation")){
				contactInformation = jb.getString("contactInformation");
			}
			
			String contactName = null;
			if (jb.containsKey("contactName")){
				contactName = jb.getString("contactName");
			}
			
			Integer publishPayType = 0;
			if(!jb.containsKey("publishPayType") && (jb.getLong("publishPayType") != 0l || jb.getLong("publishPayType") != 1l)){
				throw new RuntimeException("publishPayType is error");
 			}else{
 				publishPayType = jb.getInt("publishPayType");
			}
			
			int userGroupId = 0;
			if(jb.containsKey("userGroupId")){
				userGroupId = jb.getInt("userGroupId");
			}else {
				userGroupId = -1;
			}
			
			UserPublishInfoDTO dto = new UserPublishInfoDTO();
			dto.setUserId(userId);
			dto.setPublishType(publishType);
			dto.setPublishState(0);
			dto.setPublishPayType(publishPayType);
			dto.setPublishPrice(publishPrice);
			dto.setInfo(info);
			dto.setAddress(address);
			dto.setContactInformation(contactInformation);
			dto.setContactName(contactName);
			dto.setUserGroupId(userGroupId);
			
			sc.setData(userPublishInfoServiceImpl.add(dto));
			sc.setFlag(true);
		} catch (Exception e) {
			e.printStackTrace();
			sc.setFlag(false);
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("保存发布信息异常");
		}
		
		return sc;
	}
	
	/**取下发布接口
	 @api {post} /userPublishInfo/cancel 取消发布接口
	 
	 @apiParam {int} userId 用户编号
	 @apiParam {int} id  发布编号
	
	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 
	 @apiParamExample {json} 请求参数Demo:
	 {
		"userId":10,
		"id":5,
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 200 OK
	{
	    "flag":false,
	    "number":"详细错误信息",
	    "detailMsg":"错误错误".
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author zx
	 */
	@RequestMapping(value = "/cancel", method = RequestMethod.POST)
	@ResponseBody
	public Object cancel(@RequestBody Object body){
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body.toString());
			Integer userId = null;
			if(!jb.containsKey("userId")){
				throw new Exception("userId is null");
			}else{
				userId = jb.getInt("userId");
			}
			Integer id = null;		
			if(!jb.containsKey("id")){
				throw new Exception("id is null");
			}else{
				id = jb.getInt("id");
			}
			
			
			List<UserPublishInfoDTO> list = userPublishInfoServiceImpl
					.searchAll(userId,id, null, null, null);
			
			if(list.size() >0 && list.get(0).getOrderStatus() !=null && list.get(0).getOrderStatus() != 0 ){
				throw new RuntimeException("订单状态处于不能取消状态");
			}
			
			
			UserPublishInfoDTO dto = new UserPublishInfoDTO();
			dto.setUserId(userId);
			dto.setId(id);
			
			sc.setData(userPublishInfoServiceImpl.cancel(dto));
			sc.setFlag(true);
		} catch (Exception e) {
			e.printStackTrace();
			sc.setFlag(false);
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("取消发布信息异常");
		}
		
		return sc;
		
	}
	
	/**获取发布列表
	 @api {post} /userPublishInfo/query 获取发布列表
	 
	 @apiParam {int} currentSize 	分页起始位置
	 @apiParam {int} userId  		用户编号
	 @apiParam {int} publishType    发布类型 ，悬赏：0，分享：1
	 @apiParam {int} publishState   发布状态 已发布：0，进行中：1，完成：2，取消：3，投诉：4
	 @apiParam {int} publishId  	发布编号
	 @apiParam {int} userGroupId    小组id 没有填入-1
	 
	 
	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	 
	 @apiParamExample {json} 请求参数Demo:
	{
	"currentSize":0,
	"userId":10,
	"publishType":"5",
	"publishState":1
	"publishId":1
	"userGroupId":-1
	}
	@apiSuccessExample {json} 请求成功结果Demo:
    {
        "number": "2000",
        "flag": true,
        "data":
        [
            {
                "id": 1,
                "userId": 1,
                "publishType": 0,
                "publishPayType": 0,
                "publishPrice": "10",
                "publishState": 3,
                "createtime": 1492237413000,
                "updatetime": 1492238235000,
                "info": "帮我带一份拉面",
                "address": "一公寓401",
                "contactInformation": "15022222222",
                "contactName": "赵日天",
                "userGroupId": -1
            }
        ]
    }

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 200 OK
	{
	    "status":"error",
	    "errorMsg":"详细错误信息"
	}

 	@apiErrorExample  请求参数异常
	HTTP/1.1 400
 	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	 */
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	@ResponseBody
	public Object query(@RequestBody Object body){
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			Integer currentSize = jb.getInt("currentSize");
			if(jb.containsKey("currentSize")){
				currentSize = jb.getInt("currentSize");
			}
			Integer userId = null;
			if(jb.containsKey("userId")){
				userId = jb.getInt("userId");
			}
			Integer publishType = null;		
			if(jb.containsKey("publishType")){
				publishType = jb.getInt("publishType");
			}
			
			Integer publishState = null;
			if(jb.containsKey("publishState")){
				publishState = jb.getInt("publishState");
			}
			Integer publishId = null;
			if(jb.containsKey("publishId")){
				publishId = jb.getInt("publishId");
			}
			Integer userGroupId = null;
			if(jb.containsKey("userGroupId")){
				userGroupId = jb.getInt("userGroupId");
			}
			
			sc.setData(userPublishInfoServiceImpl.search(currentSize,userId,publishId,publishType,publishState,userGroupId));
			sc.setFlag(true);
		} catch (Exception e) {
			e.printStackTrace();
			sc.setFlag(false);
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("获取发布信息异常");
		}
		
		return sc;
	}
	
}
